/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2018
 */

package com.bm.console.business.impl;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.aliyun.oss.OSSClient;
import com.bm.console.business.WebHouseService;
import com.bm.console.common.OSSConfig;
import com.bm.console.pojo.WebHouse;
import com.bm.console.pojo.WebHouseInvestmentTypeRelation;
import com.bm.console.pojo.WebHousePhoto;
import com.bm.console.pojo.WebHouseTypeRelation;
import com.bm.console.pojo.mapper.WebHouseInvestmentTypeRelationMapper;
import com.bm.console.pojo.mapper.WebHouseMapper;
import com.bm.console.pojo.mapper.WebHousePhotoMapper;
import com.bm.console.pojo.mapper.WebHouseTypeRelationMapper;
import com.sys.console.cookie.Constant_Cookie;
import com.sys.console.pojo.Sys_User;
import com.sys.object.common.page.PageBean;
import com.sys.object.common.utils.Base64Helper;
import com.sys.object.common.utils.OSSUtil;
import org.apache.commons.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class WebHouseServiceImpl implements WebHouseService {

    @Autowired
    WebHouseMapper webHouseMapper;

    @Autowired
    WebHouseInvestmentTypeRelationMapper webHouseInvestmentTypeRelationMapper;

    @Autowired
    WebHouseTypeRelationMapper webHouseTypeRelationMapper;

    @Autowired
    private WebHousePhotoMapper webHousePhotoMapper;

    @Autowired
    private HttpServletRequest request;


    @Override
    public PageBean queryPageList(WebHouse entity, PageBean page) throws Exception {
        int rowCount = webHouseMapper.selectByPageCount(entity);
        page.setRowCount(rowCount);
        entity.setOffset(page.getStartRow());
        entity.setLimit(page.getPerpage());
        page.setList(webHouseMapper.selectByPage(entity));
        return page;
    }

    public WebHouse queryByPrimaryKey(Integer id) throws Exception {
        return webHouseMapper.selectByPrimaryKey(id);
    }

    @Transactional
    public int saveOrUpdate(WebHouse entity) throws Exception {
        Sys_User currentUser = (Sys_User)request.getSession().getAttribute(Constant_Cookie.CURRENT_USER_SESSION);
        if (!StringUtils.isEmpty(entity.getFiledata()) && !StringUtils.isEmpty(entity.getFileext())) {
            byte[] data = Base64Helper.getImageFromBASE64ToByte(entity.getFiledata());
            String filename = UUID.randomUUID() + "." + entity.getFileext();

            OSSClient ossClient = new OSSClient(OSSConfig.oss_endpoint, OSSConfig.oss_accessKeyId,
                    OSSConfig.oss_accessKeySecret);

            String eTag = OSSUtil.uploadObject2OSS(ossClient, data, filename, OSSConfig.oss_backetName, "photo/");
            ossClient.shutdown();
            if (!StringUtils.isEmpty(eTag)) {
                entity.setPhoto(OSSConfig.oss_domain + "photo/" + filename);
            }
        }
       List<String> photos_list = new ArrayList<>();
       if(!StringUtils.isEmpty(entity.getPhotos())) {
           String photos = new String(Base64.decodeBase64(entity.getPhotos().getBytes()));
           photos_list = JSON.parseObject(photos, new TypeReference<List<String>>() {});
       }


        System.err.println("house_id:"+entity.getId());
        if (entity.getId() != null && entity.getId() > 0) {
            System.err.println("update_house_id:"+entity.getId());
            entity.setUpdateTime(new Date());

            webHouseInvestmentTypeRelationMapper.deleteByHouseId(entity.getId());
            webHouseTypeRelationMapper.deleteByHouseId(entity.getId());
            if (!StringUtils.isEmpty(entity.getInvestmentTypeIds())) {
                String ids = entity.getInvestmentTypeIds();
                String[] arr = ids.split(",");
                for (int i = 0; i < arr.length; i++) {
                    WebHouseInvestmentTypeRelation webHouseInvestmentTypeRelation = new WebHouseInvestmentTypeRelation();
                    webHouseInvestmentTypeRelation.setHouseId(entity.getId());
                    webHouseInvestmentTypeRelation.setInvestmentId(Long.valueOf(arr[i]));
                    webHouseInvestmentTypeRelationMapper.insert(webHouseInvestmentTypeRelation);
                }
            }

            if (!StringUtils.isEmpty(entity.getHouseTypeIds())) {
                String ids = entity.getHouseTypeIds();
                String[] arr = ids.split(",");
                for (int i = 0; i < arr.length; i++) {
                    WebHouseTypeRelation webHouseTypeRelation = new WebHouseTypeRelation();
                    webHouseTypeRelation.setHouseId(entity.getId());
                    webHouseTypeRelation.setTypeId(Long.valueOf(arr[i]));
                    webHouseTypeRelationMapper.insert(webHouseTypeRelation);
                }
            }

            webHousePhotoMapper.deleteByHouseId(entity.getId());
            WebHousePhoto webHousePhoto = new WebHousePhoto();
            webHousePhoto.setHouseId(entity.getId());
            if(photos_list!= null && photos_list.size() > 0) {
                int sort = 1;
                for (String photo : photos_list) {
                    System.err.println(photo);
                    webHousePhoto.setId(null);
                    webHousePhoto.setPhoto(photo);
                    webHousePhoto.setCreateTime(new Date());
                    webHousePhoto.setStatus(true);
                    webHousePhoto.setSort(sort);
                    webHousePhotoMapper.insert(webHousePhoto);
                    sort++;
                }
            }

            entity.setCreateTime(new Date());
            entity.setUpdateId(currentUser.getUid());
            return this.webHouseMapper.updateByPrimaryKey(entity);
        }else {
            entity.setCreateTime(new Date());
            entity.setCreateId(currentUser.getUid());
            webHouseMapper.insert(entity);

            if (!StringUtils.isEmpty(entity.getInvestmentTypeIds())) {
                String ids = entity.getInvestmentTypeIds();
                String[] arr = ids.split(",");
                for (int i = 0; i < arr.length; i++) {
                    WebHouseInvestmentTypeRelation webHouseInvestmentTypeRelation = new WebHouseInvestmentTypeRelation();
                    webHouseInvestmentTypeRelation.setHouseId(entity.getId());
                    webHouseInvestmentTypeRelation.setInvestmentId(Long.valueOf(arr[i]));
                    webHouseInvestmentTypeRelationMapper.insert(webHouseInvestmentTypeRelation);
                }
            }

            if (!StringUtils.isEmpty(entity.getHouseTypeIds())) {
                String ids = entity.getHouseTypeIds();
                String[] arr = ids.split(",");
                for (int i = 0; i < arr.length; i++) {
                    WebHouseTypeRelation webHouseTypeRelation = new WebHouseTypeRelation();
                    webHouseTypeRelation.setHouseId(entity.getId());
                    webHouseTypeRelation.setTypeId(Long.valueOf(arr[i]));
                    webHouseTypeRelationMapper.insert(webHouseTypeRelation);
                }
            }

            WebHousePhoto webHousePhoto = new WebHousePhoto();
            webHousePhoto.setHouseId(entity.getId());
            if(photos_list!= null && photos_list.size() > 0) {
                int sort = 1;
                for (String photo : photos_list) {
                    System.err.println(photo);
                    webHousePhoto.setId(null);
                    webHousePhoto.setPhoto(photo);
                    webHousePhoto.setCreateTime(new Date());
                    webHousePhoto.setStatus(true);
                    webHousePhoto.setSort(sort);
                    webHousePhotoMapper.insert(webHousePhoto);
                    sort++;
                }
            }
        }

        return 1;
    }

    public int remove(Integer id) throws Exception {
        return this.webHouseMapper.deleteByPrimaryKey(id);
    }

    public int delete(Integer id) throws Exception {
        return this.webHouseMapper.deleteByPrimaryKey(id);
    }

    public int updateStatus(WebHouse entity) throws Exception {
        return this.webHouseMapper.updateByPrimaryKeySelective(entity);
    }

    public int updateStatus(String status, String[] ids)
            throws Exception {

        return 0;
    }


}
